package org.oneblog.data.rs;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.oneblog.data.bean.AlbumView;

public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1423103796304978330L;

    private List<T> list;
    
    private long total;
    
    private List<PageLink> pages;
    
    private List<AlbumView> albums;
    
    private Integer recommend = 0; //当搜索条件无结果时，采用站长热推文章
    
    public Integer getRecommend() {
        return recommend;
    }

    public void setRecommend(Integer recommend) {
        this.recommend = recommend;
    }

    public Result() {
        
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public List<PageLink> getPages() {
        return pages;
    }

    public void setPages(List<PageLink> pages) {
        this.pages = pages;
    }
    
    public void createPages(int currentPageNo, int pageSize) {
        int totalPages = (int) Math.ceil((double)total / pageSize);
        
        List<PageLink> links = new ArrayList<PageLink>();
        
        int minPage = currentPageNo - 3, maxPage = currentPageNo + 3;
        
        if(minPage < 1) {
            minPage = 1;
        }
        minPage = Math.min(minPage, currentPageNo);
        
        maxPage = Math.min(maxPage, totalPages);
        
        if(minPage > 1) {//当前分页最小页数大于第一页
            links.add(new PageLink(1, "首页", true));
        }
        
        for(int pageNum = minPage; pageNum <= maxPage; pageNum ++) {
            if(pageNum == currentPageNo) {
                links.add(new PageLink(pageNum, String.valueOf(pageNum), false));
            } else {
                links.add(new PageLink(pageNum, String.valueOf(pageNum), true));
            }
        }
        
        if(maxPage < totalPages) {//当前分页最大页数小于最后一页
            links.add(new PageLink(totalPages, "尾页", true));
        }
        
        setPages(links);
    }
    
    public List<AlbumView> getAlbums() {
		return albums;
	}

	public void setAlbums(List<AlbumView> albums) {
		this.albums = albums;
	}

	public static void main(String[] args) {
        Result<Object> result = new Result<>();
        result.setTotal(523);

        result.createPages(3, 20);
        
        List<PageLink> pages2 = result.getPages();
        
        for(PageLink p : pages2) {
            System.out.println(p);
        }
    }
}
